<?php
/**
 * @page perform_migration Migrating to newer version
 * 
 * We've added this new feature which will ease your process of updating to newer version. 
 * 
 * @warning Before performing migration make sure you backup your database and all files to a safe
 * place. Migration script modified data and files, in case some error occurs in middle you may loose
 * your data. So its highly recommended to download a backup as a precautionary step. 
 * 
 * Here are the steps to update to new versions:
 * 
 * 1. Make sure backup is finished, and you have downloaded the new version. Also make sure your 
 * website is not accessible by end users. 
 * 
 * 2. Upload all files from new version to your installed directory, overwriting all files. If
 * there are any local modifications, they will have to be done on new version again as they 
 * will be lost. Its recommended to prepare a list of all such modifications so as to implement 
 * them later easily.
 * 
 * 3. Open the URL www.yourwebsite.com/cms/migration/perform and press "Start" button. It will start
 * making changes in your database to update to new version.
 * 
 * @warning This tool has been added in version 1.0.11, which means you can update 1.0.11 to 1.0.12 
 * (when available), but not 1.0.10 to 1.0.11 automatically. If you have an older version, you should 
 * update manually.
 * 
 * You can also contact us for assistance on updating, if needed.
 *
 */
class MigrationController extends CmsAppController
{
	var $name = 'Migration';
	var $uses = array();


	function perform()
	{
		Configure::write('debug', 1);
		$version_db = cri("Config.AppConfig")->getVersion();
		$fileName  = ROOT.DS.'version.txt';
		$version_fs = file_get_contents($fileName);
		if(!($version_fs > $version_db))
		{
			echo "No upgrade is necessary!"; exit;
		}
		
		$this->pageTitle = "Migration Utility";
		$this->layout = 'plain';
		$checksum = md5(cr('cmsConfig.Backend.adminUser').cr('cmsConfig.Backend.adminPassword'));
		$this->set(compact('checksum'));
	}
	
	function shell($admin_checksum = 0)
	{
		$checksum = md5(cr('cmsConfig.Backend.adminUser').cr('cmsConfig.Backend.adminPassword'));
		if($admin_checksum != $checksum)
		{
			echo "Bad Checksum! Failed to proceed."; exit;
		}
		
		ob_implicit_flush(1);
		// shell call
		require_once(ROOT.DS.'app'.DS.'plugins'.DS.'cms'.DS.'vendors'.DS.'giga_web_shell_dispatcher.php');

		$argv = array(
			ROOT.DS.'cake'.DS.'console'.DS.'cake.php',
			'-working',
			APP,
			'migrate'
		);
		
		
		echo "<style>
			html
			{
			background: #000;
			color: #00FF40;
			}
			</style>";
		
		$dispatcher = new GigaWebShellDispatcher($argv);
		exit;
	}
	
	function _isAdmin()
	{
		// assume that only admin will access this controller for now
		return true;
	}

	
}

?>